package homework.qingming.calc;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FormulaScanner {

    private char leftParChar, rightParChar, funcParamsSepChar;

    private List<Character> oprs;
    private List<String> funcs;
    private List<String> varis;

    private String formula;
    private int point = 0;

    public class FormulaPart {

        private String str;
        private FormulaPartType type;

        private FormulaPart(String s, FormulaPartType type) {
            this.str = s;
            this.type = type;
        }

        public FormulaPartType getType() {
            return type;
        }

        @Override
        public String toString() {
            return str;
        }
    }

    public FormulaScanner() {
        this('(', ')', ',');
    }

    public FormulaScanner(char leftPar, char rightPar, char funcParamsSep) {
        leftParChar = leftPar;
        rightParChar = rightPar;
        funcParamsSepChar = funcParamsSep;
        oprs = new ArrayList<Character>();
        funcs = new ArrayList<String>();
        varis = new ArrayList<String>();
    }

    public FormulaScanner(char leftPar, char rightPar, char funcParamsSep,
                          Character[] operations, String[] functions) {
        leftParChar = leftPar;
        rightParChar = rightPar;
        funcParamsSepChar = funcParamsSep;
        oprs = Arrays.asList(operations);
        funcs = Arrays.asList(functions);
    }

    public FormulaScanner(Character[] operations, String[] functions) {
        this('(', ')', ',', operations, functions);
        oprs = Arrays.asList(operations);
        funcs = Arrays.asList(functions);
    }

    public boolean hasNext() {
        return point < formula.length();
    }

    public FormulaPart next() throws ParseException {
        FormulaPart result = null;
        while(point<formula.length() && result == null) {
            Character c = formula.charAt(point);
            if(c == ' ' || c == '\n' || c == '\t') {
                point++;
                continue;
            }

            if(c == leftParChar || c == rightParChar || c == funcParamsSepChar) {
                result = new FormulaPart(String.valueOf(c), FormulaPartType.SYMBOL);
                point++;
            } else if(hasOperation(c)) {
                result = new FormulaPart(String.valueOf(c), FormulaPartType.OPERATOR);
                point++;
            } else if(isAlpha(c)) {
                StringBuilder builder = new StringBuilder();
                for(;point<formula.length() && isAlpha(c = formula.charAt(point));point++) {
                    builder.append(c);
                }
                String str = builder.toString();
                if(c == leftParChar) {
                    if(funcs.indexOf(str) < 0) {
                        throw new ParseException("错误表达式: "+str, point-str.length());
                    }
                    // 跳过左括号
                    point++;
                    result = new FormulaPart(str, FormulaPartType.FUNCTION);
                } else if(varis.indexOf(str) >= 0) {
                    result = new FormulaPart(str, FormulaPartType.VARIABLE);
                } else {
                    throw new ParseException("错误表达式: "+str, point-str.length());
                }
            } else if(Character.isDigit(c)) {
                StringBuilder builder = new StringBuilder();
                int pointCount = 0;
                for(;point<formula.length() && (Character.isDigit(c = formula.charAt(point)) || c == '.');point++) {
                    if(c == '.') pointCount++;
                    builder.append(c);
                }
                String numStr = builder.toString();
                if(pointCount >= 2) throw new NumberFormatException(numStr);
                result = new FormulaPart(numStr, FormulaPartType.NUMBER);
            } else {
                throw new ParseException("错误字符: "+c, point-1);
            }
        }
        return result;
    }

    public void fromString(String str) {
//        formula = String.join("", str.split(" "));
        formula = str;
        point = 0;
    }

    public void addOperation(char opr) {
        oprs.add(opr);
    }

    public void addFunction(String func) {
        funcs.add(func);
    }

    public void removeOperation(char opr) {
        oprs.remove(opr);
    }

    public void removeFunction(String func) {
        funcs.remove(func);
    }

    public void addVariable(String name) {
        varis.add(name);
    }

    public void removeVariable(String name) {
        varis.remove(name);
    }

    public boolean hasOperation(char opr) {
        return oprs.indexOf(opr) != -1;
    }

    public boolean hasOperation(String oprStr) {
        return oprStr.length() == 1 && hasOperation(oprStr.charAt(0));
    }

    public boolean isAlpha(char c) {
        return c >= 97 && c <= 122 || (c >= 65 && c <= 90);
    }
}
